@echo off

setlocal enabledelayedexpansion

set i=0
for /f "skip=1 tokens=1" %%a in ('adb devices') do (
    set devices[!i!]=%%a
    set /a i+=1
)

set /a i-=1

set index=0
for /l %%i in (0,1,%i%) do (
    @REM echo !devices[%%i]!
    set /a index=%%i
    set "device=!devices[%%i]!"
    for /f "tokens=*" %%x in ('adb -s !device! shell getprop ro.product.model') do (
        set "models[!index!]=%%x"
    )
)

set "cmdArgs=%*"
if %i% GTR 0 (
    for /l %%i in (0,1,%i%) do (
        echo !devices[%%i]! : !models[%%i]!
    )
    set /p dIndex=more than one device, select one:
    @REM adb -s !devices[%dIndex%]! %cmdArgs%
    set /a count=0
    set "newArgs="
    :loop
    if "%1"=="" goto run_adb
    set /a count+=1
    if !count!==1 (
        set "newArgs=%1"
    ) else (
        set "newArgs=!newArgs! %1"
    )
    shift
    goto loop
    :run_adb
    adb -s !devices[%dIndex%]! !newArgs!
    goto end
) else (
    adb %*
)

:end
